package com.xxd.spring.boot.demo.wubolin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxd.spring.boot.demo.wubolin.entity.Curriculum;
import com.xxd.spring.boot.demo.wubolin.entity.Grade;
import com.xxd.spring.boot.demo.wubolin.exception.MyException;
import com.xxd.spring.boot.demo.wubolin.mapper.CurriculumMapper;
import com.xxd.spring.boot.demo.wubolin.service.CurriculumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class CurriculumServiceImpl implements CurriculumService {

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Override
    public int add(Curriculum curriculum) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("nickname",curriculum.getNickname());
        Curriculum dbCurr = curriculumMapper.selectOne(queryWrapper);
        if(dbCurr != null){
            throw new MyException("学科已存在","123");
        }
        int  insert = curriculumMapper.insert(curriculum);
        return insert;
    }


    @Override
    public int update(Curriculum curriculum) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("nickname",curriculum.getNickname());
        Curriculum dbCurr = curriculumMapper.selectOne(queryWrapper);
        if(dbCurr != null){
            throw new MyException("学科不存在","123");
        }
        return curriculumMapper.updateById(curriculum);
    }

    @Override
    public int delete(Integer id) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Curriculum dbCurr = curriculumMapper.selectOne(queryWrapper);
        if(dbCurr != null){
            throw new MyException("学科不存在","123");
        }
        return curriculumMapper.deleteById(id);
    }

    @Override
    public IPage<Curriculum> listPage(Page page, Map<String, Object> params) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(params);
        Page resPage = curriculumMapper.selectPage(page,queryWrapper);
        if (resPage == null || resPage.getSize() == 0){
            throw new MyException("学科不存在","123");
        }
        return resPage;
    }

    @Override
    public List list(Map<String, Object> params) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(params);
        List list = curriculumMapper.selectList(queryWrapper);
        if(list == null || list.isEmpty()){
            throw new MyException("学科不存在","123");
        }
        return list;
    }

    @Override
    public List<Curriculum> all() {
        return curriculumMapper.all();
    }
}
